#include "customchartview.h"

CustomChartView::CustomChartView(QWidget *parent)
    : QChartView (parent)
{
    /* 初始化 */
    sChart = nullptr;
    previousCursor = -1;
}

CustomChartView::~CustomChartView(){

}

/* 改变鼠标指针样式 */
void CustomChartView::changeMouseArrowShape(ArrowShape arrowShape){
    if(previousCursor == -1){ // 初始化指针变量
        previousCursor = arrowShape;
    }else if(previousCursor == arrowShape){ // 指针变量没变
        return;
    }

    switch (arrowShape) {
    case 1: // 箭头形状
        this->viewport()->setCursor(Qt::ArrowCursor);
        break;
    case 2: // 手指形状
        this->viewport()->setCursor(Qt::PointingHandCursor);
        break;
    }
    previousCursor = arrowShape; // 更新指针变量
}

/* 鼠标按下事件（重写） */
void CustomChartView::mousePressEvent(QMouseEvent *event){
    emit CustomChartView::chartViewMousePress(event);
//    qDebug() << "---------------"  << event << &event;
}

/* 鼠标移动事件（重写） */
void CustomChartView::mouseMoveEvent(QMouseEvent *event){
    emit CustomChartView::chartViewMouseMove(event);
}

/* 鼠标抬起事件（重写） */
void CustomChartView::mouseReleaseEvent(QMouseEvent *event){
    emit CustomChartView::chartViewMouseRelease(event);
}

/* 窗口大小改变事件（重写） */
void CustomChartView::resizeEvent(QResizeEvent *event){
    this->chart()->setGeometry(0, 0, this->geometry().width(), this->geometry().height());
    emit chartViewResize(event);
}

void CustomChartView::mouseDoubleClickEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
        //获取鼠标位置
        QPoint pos = event->pos();
        //将鼠标位置转换为图表坐标系的值
        QPointF val = chart()->mapToValue(pos);
        //打印鼠标位置和值
        qDebug() << "Mouse position:" << pos;
        qDebug() << "Chart value:" << val;
    }
   emit CustomChartView::chartViewDoubleClick(event);
}



